package com.example.team.dao.Imp;

import com.example.team.dao.IPresenceinformationDao;
import com.example.team.entity.Presenceinformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 作者:王轩
 * date: 2023/10/31
 **/
@Repository
public class IPresenceinformationDaoImpl implements IPresenceinformationDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertPresenceinformation(Presenceinformation presenceinformation) {
        return jdbcTemplate.update("insert into presenceinformation(EmployeeID,PresenceDate,Presence,Matters,EmployeeName,PresenceID)"+"value(?,?,?,?,?,?)",
                presenceinformation.getEmployeeID(),
                presenceinformation.getPresenceDate(),
                presenceinformation.getPresence(),
                presenceinformation.getMatters(),
                presenceinformation.getEmployeeName(),
                presenceinformation.getPresenceID());
    }

    @Override
    public int deletePresenceinformation(Presenceinformation presenceinformation) {
        return jdbcTemplate.update("delete from presenceinformation where PresenceID=?",presenceinformation.getPresenceID());
    }

    @Override
    public int updatePresenceinformation(Presenceinformation presenceinformation) {
        return jdbcTemplate.update("update presenceinformation set EmployeeID=?,PresenceDate=?,Presence=?,Matters=?,EmployeeName=? where PresenceID=?",
                presenceinformation.getEmployeeID(),
                presenceinformation.getPresenceDate(),
                presenceinformation.getPresence(),
                presenceinformation.getMatters(),
                presenceinformation.getEmployeeName(),
                presenceinformation.getPresenceID());
    }

    @Override
    public List<Presenceinformation> getAllPresenceinformation(Presenceinformation presenceinformation) {
        return jdbcTemplate.query("select * from presenceinformation ",new Object[]{},new BeanPropertyRowMapper<>(Presenceinformation.class));
    }

    @Override
    public Presenceinformation getPresenceinformation(Presenceinformation presenceinformation) {
        List<Presenceinformation> result=jdbcTemplate.query("select " +
                        "*" +
                        "from presenceinformation where PresenceID='"+presenceinformation.getPresenceID()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Presenceinformation.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
